import base64
from io import BytesIO
# from datetime import date,datetime
# pip3 install pillow
import datetime
from PIL import Image


# 若img.save()报错 cannot write mode RGBA as JPEG
# 则img = Image.open(image_path).convert('RGB')
def image_to_base64(image_path):
    img = Image.open(image_path)
    output_buffer = BytesIO()
    img.save(output_buffer, format='PNG')
    byte_data = output_buffer.getvalue()
    base64_str = base64.b64encode(byte_data)
    print(base64_str)
    return base64_str


class Pagination:
    """
    explain:
        obj = Pagination(1000, 20, 50)
        print(obj.start)
        print(obj.end)
        obj.item_pages --> 求分页的页码
    all_item :need the query library to count
    """
    """
    all_item: 总count
    current_page: 你的页数
    appear_page： 每页多少条数据
    """

    def __init__(self, all_item, appear_page=50):
        self.appear_page = appear_page
        self.all_item = all_item
        self.page = all_item // appear_page if all_item % appear_page == 0 else all_item // appear_page + 1


# 点击某一分类，返回数据
def get_api_date():
    '''
    code = 0 表示成功请求
    '''
    # 去数据库中查询数据，得出总数，假设999条，每页显示10个
    count = 999
    page_size = 10
    page = count // page_size if count % page_size == 0 else count // page_size + 1
    return page


def show_data(page, limit: int = 10):
    '''
    page: 第几页
    limit: 默认获取10条数据，想获取几条数据
    '''
    start = (page-1)*limit
    limit = limit
    if 'key的剩余次数'<'查询数据得到的数量':
        pass
    # key的使用次数减少量：查询数据库中得到的数量


    # 返回数据给前端并展示
    return {
        'code': 0,
        'data' : [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
    }

print(len(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']))


if __name__ == '__main__':
    obj = Pagination(1001)
